package com.ds.lens.data.common.asserts;

import com.ds.lens.data.common.error.BusinessException;
import com.ds.lens.data.common.error.Code;

import java.util.Collection;
import java.util.Map;

/**
 * Description:
 * 断言
 *
 * @author WeiShaoying
 * @date 2019-07-15
 */
public class Asserts {
    private Asserts() {
    }

    private static void assertArgument(boolean value, Code code, Object... args) {
        if (!value) {
            throw new BusinessException(code, args);
        }
    }

    public static void assertTrue(Boolean value, Code code, Object... args) {
        assertArgument(Boolean.TRUE.equals(value), code, args);
    }

    public static void assertFalse(Boolean value, Code code, Object... args) {
        assertArgument(Boolean.FALSE.equals(value), code, args);
    }

    public static void assertNotNull(Object obj, Code code, Object... args) {
        assertArgument(null != obj, code, args);
    }

    public static void asserNull(Object obj, Code code, Object... args) {
        assertArgument(null == obj, code, args);
    }

    public static <T extends Map<?, ?>> T assertNotEmpty(T map, Code code, Object... args) {
        assertArgument((null != map && !map.isEmpty()), code, args);
        return map;
    }

    public static <T extends Collection<?>> T assertNotEmpty(T collection, Code code, Object... args) {
        assertArgument(collection != null && !collection.isEmpty(), code, args);
        return collection;
    }

    public static String assertNotEmpty(String str, Code code, Object... args) {
        assertArgument(str != null && !str.isEmpty(), code, args);
        return str;
    }

    public static String assertNotBlank(String str, Code code, Object... args) {
        assertArgument(str != null && !str.isEmpty() && !str.trim().isEmpty(), code, args);
        return str;
    }

    public static int[] assertNotEmpty(int[] array, Code code, Object... args) {
        assertArgument(array != null && array.length > 0, code, args);
        return array;
    }

    public static <T> T[] assertNotEmpty(T[] array, Code code, Object... args) {
        assertArgument(array != null && array.length > 0, code, args);
        return array;
    }
}
